#! /bin/bash
#
# script to insert the modules for the nudaq card for various OS levels
# and to create device file entries if necessary
#
# 21.9.2006,  Christian Kurtsiefer, phyck@nus.edu.sg
# 12.3.07     compatibility check with 10.2 suse distro
# 31.3.07     changed double loading feature due to irq problem
#
# standard location: /etc/init.d/
# source path form moduls may have to be adapted
#

# attempt to make it a LSB compliant service control script;
# see http://www.linuxbase.org/spec/

### BEGIN INIT INFO
# Provides: nudaqcard
# Required-Start:
# Should-Start: resmgr
# Required-Stop:
# Should-Stop: resmgr
# Default-Start:  3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: nudaq PC7200 card driver
# Description: Device driver for the Nudaq PC7200
#	 data acquisition card. Works with one card
#        for the moment, may be tested with several
#        cards.
### END INIT INFO

# . /etc/rc.status
osversion=$(uname -r  |sed  "s/\([0-9].[0-9]\).*/\\1/")

# original prober
# iodriverpath="/home/chris/programs/nudaq_driver/"$osversion

# new thing, gets replaced by a makefile
iodriverpath=DRIVERPATH/$osversion

dev3="nudaq_dmaread_special"
dev2="nudaq_dmaread"

group="users"

if test $osversion == "2.4";
    then module="ndd2.4-3";driver=$module".o"; 
else module="ndd2p6";driver=$module".ko"
fi

return=$rc_done

case "$1" in
     start)
	echo "inserting nudaq PCI7200 card driver module ($driver)..."
	insmod $iodriverpath/$driver || return=$rc_failed
	rmmod $module || return=$rc_failed
	insmod $iodriverpath/$driver || return=$rc_failed

	major=$(awk "\$2==\"$module\"  {print \$1}" /proc/devices)
	echo "used major device number $major for nudaq driver"

# remove stale device files
	rm -f /dev/ioboards/nudaq*
	
# create directories and file entries
	mkdir -p /dev/ioboards
	mknod /dev/ioboards/${dev3} c $major 3
	mknod /dev/ioboards/${dev2} c $major 2

# set permission and group (should be more secure..)
	chgrp $group /dev/ioboards/${dev3}
	chmod 664 /dev/ioboards/${dev3}
	chgrp $group /dev/ioboards/${dev2}
	chmod 664 /dev/ioboards/${dev2}

	echo "created device file /dev/ioboards/${dev3} and ${dev2}"

	echo -e "$return"
	;;
     stop)
	echo "removing nudaq pci7200 module..."
	rmmod $module || return=$rc_failed
	echo -e "$return"
	;;
     restart|reload)
	$0 stop && $0 start || return=$rc_failed
	;;
     *)
        echo "Usage: $0 {start|stop}"
	exit 1
esac

test "$return" = "$rc_done" || exit 1
exit 0
